/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager.search;

import br.com.foxline.clinicplus.entity.*;
import br.com.foxline.clinicplus.service.*;
import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.DualListModel;

/**
 *
 * @author ely
 */
@ManagedBean
@ViewScoped
public class ManagerSearchUser implements Serializable {

    @EJB
    SystemUserService userService;
    @EJB
    GroupService groupService;
    private SystemUser user;
    private String confirm;
    private String userEdit;
    private String userShow;
    private String userAcess;
    private SystemUser searchUser;
    private List<SystemUser> users;
    private DualListModel<Groups> dualGrupo;
    private List<Groups> groups;
    private String passwordOld;
    private String passwordNew;
    private SystemUser showUser;
    private List<Groups> showGroups;
    private SystemUser acessUser;

    @PostConstruct
    public void init() {
        searchUser = new SystemUser();
        users = new ArrayList<SystemUser>();

        dualGrupo = new DualListModel<Groups>();

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        userEdit = params.get("userEdit");
        userShow = params.get("userShow");
        userAcess = params.get("userAcess");
        paramPost();
    }

    public void update() {
        if (passwordOld != null && passwordNew != null && confirm != null) {
            userService.updateUser(confirm, passwordOld, passwordNew, user);
        }else{
            userService.update(user);
        }
    }

    public void updateAcess() {
        acessUser.setGroups(dualGrupo.getTarget());
        userService.updateAcess(acessUser);
    }

    public void search() {
        users = userService.findUsers(searchUser);
    }

    public void paramPost() {
        try {
            if (userShow != null) {
                showUser = userService.find(Long.parseLong(userShow));
                showGroups = groupService.findGroups(showUser);
                showUser.setGroups(showGroups);
            } else if (userEdit != null) {
                user = userService.find(Long.parseLong(userEdit));

            } else if (userAcess != null) {
                acessUser = userService.find(Long.parseLong(userAcess));
                groups = groupService.findGroups(acessUser);
                acessUser.setGroups(groups);
                List<Groups> lst = groupService.findAll();
                lst.removeAll(acessUser.getGroups());
                dualGrupo = new DualListModel<Groups>(lst, acessUser.getGroups());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SystemUser getUser() {
        return user;
    }

    public void setUser(SystemUser user) {
        this.user = user;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public DualListModel<Groups> getDualGrupo() {
        return dualGrupo;
    }

    public void setDualGrupo(DualListModel<Groups> dualGrupo) {
        this.dualGrupo = dualGrupo;
    }

    public SystemUser getSearchUser() {
        return searchUser;
    }

    public void setSearchUser(SystemUser searchUser) {
        this.searchUser = searchUser;
    }

    public List<SystemUser> getUsers() {
        return users;
    }

    public void setUsers(List<SystemUser> users) {
        this.users = users;
    }

    public String getPasswordOld() {
        return passwordOld;
    }

    public void setPasswordOld(String passwordOld) {
        this.passwordOld = passwordOld;
    }

    public String getPasswordNew() {
        return passwordNew;
    }

    public void setPasswordNew(String passwordNew) {
        this.passwordNew = passwordNew;
    }

    public SystemUser getShowUser() {
        return showUser;
    }

    public void setShowUser(SystemUser showUser) {
        this.showUser = showUser;
    }

    public List<Groups> getShowGroups() {
        return showGroups;
    }

    public void setShowGroups(List<Groups> showGroups) {
        this.showGroups = showGroups;
    }

    public SystemUser getAcessUser() {
        return acessUser;
    }

    public void setAcessUser(SystemUser acessUser) {
        this.acessUser = acessUser;
    }
    
}
